﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace kairiseiMAParser
{
    public partial class Form1 : Form
    {
        public Dictionary<string, Enemy> enemyData = new Dictionary<string, Enemy>();
        public Dictionary<string, Enemy_lvup> enemy_lvupData = new Dictionary<string, Enemy_lvup>();
        public Dictionary<string, Enemy_ai_order> enemy_ai_orderData = new Dictionary<string, Enemy_ai_order>();
        public Dictionary<string, Skill> skillData = new Dictionary<string, Skill>();
        public List<Skill_roleData> skill_roleData = new List<Skill_roleData>();
        public List<string> enemyid = new List<string>();

        public class Skill_roleData
        {
            public string id;
            public skill_role skill_role;

            public Skill_roleData(string id, skill_role skill_role)
            {
                this.id = id;
                this.skill_role = skill_role;
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = GetEnemyByID(textBox1.Text);
            if (id != null)
            {
                enemyid.Add(id);
                listBox1.Items.Add(enemyData[id].Name);
            }
        }

        public string GetEnemyByID(string ID)
        {
            foreach (var item in enemyData)
            {
                if (item.Value.ID == ID)
                {
                    return item.Value.ID;
                }
            }
            return null;
        }

        public void parseEnemy(string EnemyID)
        {
            textBox2.AppendText("名称：" + enemyData[EnemyID].Name + "\r\n");
            textBox2.AppendText("属性：" + enemyData[EnemyID].Attribute + "\r\n");
            textBox2.AppendText("HP：" + enemyData[EnemyID].HP.ToString() + "\r\n");
            textBox2.AppendText("ATK：" + enemyData[EnemyID].ATK.ToString() + "\r\n");
            textBox2.AppendText("INT：" + enemyData[EnemyID].INT.ToString() + "\r\n");
            textBox2.AppendText("MND：" + enemyData[EnemyID].MND.ToString() + "\r\n");
            textBox2.AppendText("DEF：" + enemyData[EnemyID].DEF.ToString() + "\r\n");
            textBox2.AppendText("MDEF：" + enemyData[EnemyID].MDEF.ToString() + "\r\n");
            textBox2.AppendText("减伤：" + enemyData[EnemyID].SubtractionDamage.ToString() + "\r\n");
            List<Enemy_lvup.Skills> allskill = new List<Enemy_lvup.Skills>();
            Enemy_lvup data1 = enemy_lvupData[EnemyID];
            textBox2.AppendText("每回合行动：" + data1.OneTurnActions + "\r\n");
            textBox2.AppendText("属性倍率：\r\n");
            textBox2.AppendText("火：" + data1.Fire + "\r\n");
            textBox2.AppendText("冰：" + data1.Ice + "\r\n");
            textBox2.AppendText("风：" + data1.Wind + "\r\n");
            textBox2.AppendText("光：" + data1.Light + "\r\n");
            textBox2.AppendText("暗：" + data1.Dark + "\r\n");
            if (data1.normalattack.AIID != "")
            {
                allskill.Add(data1.normalattack);
            }
            foreach (var key in data1.skill)
            {
                if (key.AIID != "")
                {
                    allskill.Add(key);
                }
            }
            foreach (var key in data1.bigskill)
            {
                if (key.AIID != "")
                {
                    allskill.Add(key);
                }
            }
            //根据优先度排序
            if (radioButton1.Checked)
            {
                var query = from items in allskill orderby Convert.ToInt32(items.Priority) select items;
                foreach (var i in query)
                {
                    ShowSkill(i);
                }
            }
            else if (radioButton2.Checked)
            {
                var query = from items in allskill orderby skillData[items.SkillID].id select items;
                foreach (var i in query)
                {
                    ShowSkill(i);
                }
            }
            textBox4.Select(0, 0);
            textBox4.ScrollToCaret();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox4.Clear();
            if (listBox1.SelectedIndex != -1)
            {
                string id = enemyid[listBox1.SelectedIndex];
                parseEnemy(id);
            }
        }

        public void ShowSkill(Enemy_lvup.Skills skillinfo)
        {
            var AI = enemy_ai_orderData[skillinfo.AIID];
            var skill = skillData[skillinfo.SkillID];
            Enemy_ai_order aioder = enemy_ai_orderData[skillinfo.AIID];
            List<skill_role> role = getskill_roleids(skillinfo.SkillID);
            textBox4.AppendText("技能名：" + skill.name + "(" + skill.id + "+" + skillinfo.AIID + ")\r\n");
            textBox4.AppendText("副名：" + skill.sub_name + "\r\n");
            textBox4.AppendText("说明：" + skill.explan + "\r\n");
            //textBox4.AppendText("技能类型：" + skill.kind + "\r\n");
            textBox4.AppendText("属性：" + skill.attr + "\r\n");
            textBox4.AppendText("物理or魔法：" + skill.physics_type + "\r\n");
            textBox4.AppendText("作用目标：" + skill.target + "\r\n");
            textBox4.AppendText("优先度：" + skillinfo.Priority + "\r\n");
            textBox4.AppendText("目标选择：" + skillinfo.TargetType + "\r\n");
            textBox4.AppendText("消费行动：" + skillinfo.Cost + "\r\n");
            textBox4.AppendText("最大发动次数：" + skillinfo.Maximum + "\r\n");
            textBox4.AppendText("发动率：" + skillinfo.Rate + "%\r\n");
            if (AI.PartCondition != "NULL")
            {
                textBox4.AppendText("部位触发条件：" + Tools.ENEMY_AI_ORDER_PARTY(AI.PartCondition) + "\r\n");
            }
            if (AI.SelfHPInfimum != "")
            {
                textBox4.AppendText("部位HP触发条件：上限" + AI.SelfHPMaximum + "% 下限" + AI.SelfHPInfimum + "%\r\n");
            }
            if (AI.BodyHPInfimum != "")
            {
                textBox4.AppendText("本体HP触发条件：上限" + AI.BodyHPMaximum + "% 下限" + AI.BodyHPInfimum + "%\r\n");
            }
            if (AI.TriggerCondition != "NULL")
            {
                textBox4.AppendText("攻击触发条件：" + Tools.ENEMY_AI_ORDER_TRIGGER(AI.TriggerCondition));
                if (AI.Parameter1 != "")
                {
                    textBox4.AppendText(" " + Tools.ENEMY_AI_ORDER_TRIGGER(AI.Parameter1));
                }
                if (AI.Parameter2 != "")
                {
                    textBox4.AppendText(" " + Tools.ENEMY_AI_ORDER_TRIGGER(AI.Parameter2));
                }
                if (AI.Parameter3 != "")
                {
                    textBox4.AppendText(" " + Tools.ENEMY_AI_ORDER_TRIGGER(AI.Parameter3));
                }
                textBox4.AppendText("\r\n");
            }
            ShowRole(role);
            textBox4.AppendText("发动回合：");
            for (int j = 0; j < aioder.Turn.Length; j++)
            {
                if (aioder.Turn[j] == 1)
                {
                    textBox4.AppendText((j + 1) + "T ");
                }
            }
            textBox4.AppendText("\r\n");
            if (skill.extend.order != "")
            {
                textBox4.AppendText("特殊：\r\n");
                textBox4.AppendText("条件：" + skill.extend.order + "\r\n");
                textBox4.AppendText("参数：");
                for (int i = 0; i < 5; i++)
                {
                    if (skill.extend.order_params[i] != "")
                    {
                        textBox4.AppendText(skill.extend.order_params[i] + " ");
                    }
                }
                textBox4.AppendText("\r\n機能ID：" + skill.extend.roleid);
                textBox4.AppendText("\r\n");
                role = getskill_roleids(skill.extend.roleid.ToString());
                ShowRole(role);
            }
            textBox4.AppendText("\r\n\r\n");
        }

        private void ShowRole(List<skill_role> role)
        {
            foreach (var i in role)
            {
                string str = Tools.getskill_role(i.type, i.parameter);
                if (str != null)
                {
                    textBox4.AppendText("真·技能说明：");
                    textBox4.AppendText(str + "\r\n");
                    textBox4.AppendText("真·作用目标：");
                    textBox4.AppendText(i.target + "\r\n");

                }
                else
                {
                    textBox4.AppendText("技能参数类型：" + i.type + "\r\n");
                    textBox4.AppendText("相关参数：");
                    for (int j = 0; j < 10; j++)
                    {
                        if (i.parameter[j] != "")
                        {
                            textBox4.AppendText(i.parameter[j] + "  ");
                        }
                    }
                    textBox4.AppendText("\r\n作用目标：" + i.target + "\r\n");
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists("enemy.csv"))
                {
                    MessageBox.Show("缺少enemy.csv");
                    this.Close();
                    return;
                }
                if (!File.Exists("enemy_ai_order.csv"))
                {
                    MessageBox.Show("缺少enemy.csv");
                    this.Close();
                    return;
                }
                if (!File.Exists("enemy_lvup.csv"))
                {
                    MessageBox.Show("缺少enemy.csv");
                    this.Close();
                    return;
                }
                if (!File.Exists("skill.csv"))
                {
                    MessageBox.Show("缺少enemy.csv");
                    this.Close();
                    return;
                }
                //载入enemy.csv
                string[] str = File.ReadAllLines("enemy.csv", Encoding.Default);
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i].Length == 0 || str[i].Substring(0, 1) == "#")
                    {
                        continue;
                    }
                    string[] parts = str[i].Split(new string[] { "," }, StringSplitOptions.None);
                    enemyData[parts[0]] = new Enemy(parts);
                }
                //载入enemy_lvup.csv
                string[] str2 = File.ReadAllLines("enemy_lvup.csv", Encoding.Default);
                for (int i = 0; i < str2.Length; i++)
                {
                    if (str2[i].Length > 0 && str2[i].Substring(0, 1) == "#")
                    {
                        continue;
                    }
                    else if (str2[i].Length > 0 && str2[i].Substring(0, 1) == ",")
                    {
                        continue;
                    }
                    else if (str2[i].Length == 0)
                    {
                        continue;
                    }
                    string[] parts = str2[i].Split(new string[] { "," }, StringSplitOptions.None);
                    enemy_lvupData[parts[0]] = new Enemy_lvup(parts);
                }
                //载入enemy_ai_order.csv
                string[] str3 = File.ReadAllLines("enemy_ai_order.csv", Encoding.Default);
                for (int i = 0; i < str3.Length; i++)
                {
                    if (str3[i].Length == 0 || str3[i].Substring(0, 1) == "#")
                    {
                        continue;
                    }
                    string[] parts = str3[i].Split(new string[] { "," }, StringSplitOptions.None);
                    enemy_ai_orderData[parts[0]] = new Enemy_ai_order(parts);
                }
                //载入skill.csv
                string[] str4 = File.ReadAllLines("skill.csv", Encoding.Default);
                for (int i = 0; i < str4.Length; i++)
                {
                    if (str4[i].Length == 0 || str4[i].Substring(0, 1) == "#")
                    {
                        continue;
                    }
                    string[] parts = str4[i].Split(new string[] { "," }, StringSplitOptions.None);
                    skillData[parts[0]] = new Skill(parts);
                }
                //载入skill_role.csv
                string[] str5 = File.ReadAllLines("skill_role.csv", Encoding.Default);
                for (int i = 0; i < str5.Length; i++)
                {
                    if (str5[i].Length == 0 || str5[i].Substring(0, 1) == "#")
                    {
                        continue;
                    }
                    string[] parts = str5[i].Split(new string[] { "," }, StringSplitOptions.None);
                    skill_roleData.Add(new Skill_roleData(parts[0], new skill_role(parts)));
                }
                //载入完成
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
                this.Close();
                return;
            }
        }


        public List<skill_role> getskill_roleids(string skillid)
        {
            List<skill_role> re = new List<skill_role>();
            foreach (var i in skill_roleData)
            {
                if (i.id == skillid)
                {
                    re.Add(i.skill_role);
                }
            }
            return re;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            listBox1.SetSelected(listBox1.SelectedIndex, true);
        }
    }
}
